package com.company.project.service.impl;

import com.company.project.core.Result;
import com.company.project.core.ResultCode;
import com.company.project.core.ResultGenerator;
import com.company.project.dao.FoodMapper;
import com.company.project.model.AdminLog;
import com.company.project.model.Cart;
import com.company.project.model.Food;
import com.company.project.model.request.QueryParams;
import com.company.project.model.response.PageList;
import com.company.project.service.FoodService;
import com.company.project.core.AbstractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;


/**
 * Created by CodeGenerator on 2020/04/21.
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class FoodServiceImpl extends AbstractService<Food> implements FoodService {
    @Resource
    private FoodMapper foodMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public Result delCartFood(Cart entity) {
        if(entity.getUserId() == 0) {
            if(redisTemplate.hasKey("cart")) {
                List<String> cartList = (List<String>) redisTemplate.opsForList().leftPop("cart");
                cartList.removeIf(foodId -> foodId.equals(entity.getFoodId().toString()));
                redisTemplate.opsForList().leftPush("cart", cartList);
            }
        } else {
            if(redisTemplate.hasKey(entity.getUserId().toString())) {
                List<String> cartList = (List<String>) redisTemplate.opsForList().leftPop(entity.getUserId().toString());
                cartList.removeIf(foodId -> foodId.equals(entity.getFoodId().toString()));
                redisTemplate.opsForList().leftPush(entity.getUserId().toString(), cartList);
            }
        }
        return ResultGenerator.genSuccessResult();
    }

    @Override
    public Result list(QueryParams queryParams) {
        int page = queryParams.getPage();
        int size = queryParams.getSize();
        queryParams.setStart((page - 1) * size);
        List<AdminLog> list = foodMapper.getFoodList(queryParams);
        Integer total = foodMapper.getFoodListTotal(queryParams);
        PageList<AdminLog> pageList = new PageList<>();
        pageList.setList(list);
        pageList.setTotal(total);
        return ResultGenerator.genSuccessResult(pageList);
    }

    @Override
    public void deleteOne(Food food) {
        foodMapper.delete(food);
    }

    @Override
    public Result updateFood(Food food) {
        Food query = new Food();
        query.setFoodId(food.getFoodId());
        Food entity = foodMapper.selectOne(query);
        entity.setCate(food.getCate());
        entity.setImg(food.getImg());
        entity.setName(food.getName());
        entity.setSellPrice(food.getSellPrice());
        foodMapper.updateByPrimaryKey(entity);
        return ResultGenerator.genSuccessResult();
    }

    @Override
    public Result add(Food food) {
        food.setStatus("1");
        food.setCreatTime(new Date());
        food.setTotalSales(0);
        food.setLikeNum(0);
        foodMapper.insert(food);
        return ResultGenerator.genSuccessResult();
    }
}
